﻿using System;
using System.Windows;
using Microsoft.Practices.Prism.Regions;
using Tanmia.Basis.ViewModel;

namespace Tanmia.Basis.Prism
{
    public static class PrismExtensions
    {
        public static T GetParameter<T>(this NavigationContext navContext, string paramName)
            where T : class
        {
            NavigationUri navUri = navContext.Uri as NavigationUri;
            if (navUri != null)
            {
                if (navUri.Parameters.ContainsKey(paramName))
                    return navUri.Parameters[paramName] as T;

            }

            return null;
        }

        public static void RequestNavigate(this IRegionManager regionManager, string regionName, string source, params object[] nameValueParisParam)
        {
            if (nameValueParisParam.Length % 2 != 0)
                throw new ArgumentOutOfRangeException("nameValueParisParam.Length must accept 2 division");

            NavigationUri navUri = new NavigationUri(source);

            for (int i = 0; i < nameValueParisParam.Length; i += 2)
                navUri.Parameters[nameValueParisParam[i].ToString()] = nameValueParisParam[i + 1];

            regionManager.RequestNavigate(regionName, navUri);
        }

        /// <summary>
        /// Navigate to the editor view.
        /// You are allowed to navigate to the same editorView more the once.
        /// PS. The instance of entity parameter must not passed to more than one calling.
        /// </summary>
        /// <param name="regionManager"></param>
        /// <param name="editorView">the editor view name, which is registed in the container.</param>
        /// <param name="entity">Business entity to manpulated in the editor view.</param>
        /// <exception cref="System.ArgumentNullException">When entity parameter is passed null.</exception>
        public static void NavigateToEditor(this IRegionManager regionManager, string editorView, object entity)
        {
            NavigationUri navUri = new NavigationUri(editorView);
            navUri.Parameters[Constants.EntityParam] = entity;
            regionManager.RequestNavigate(Constants.RegionEditor, navUri);
        }

        public static void AddToWindowRegion(this IRegionManager regionManager, object editorView, object entity)
        {

        }

        /// <summary>
        /// Find the view object which had created the passed viewModel.
        /// </summary>
        /// <param name="regionManager"></param>
        /// <param name="viewModel">viewModel which is associated with the missing view.</param>
        /// <returns>view object if is found, null otherwise.</returns>
        public static object GetView(this IRegionManager regionManager, BaseViewModel viewModel)
        {
            if (viewModel == null)
                throw new ArgumentNullException("viewModel");

            foreach (IRegion region in regionManager.Regions)
                foreach (FrameworkElement view in region.Views)
                    if (view.DataContext == viewModel)
                        return view;

            return null;
        }
    }
}
